﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Amoo
{
    public partial class _default : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                MantemLogin();

                if (!Page.IsPostBack)
                {

                }

                if (Request["idComunidade"] != null)
                {
                    MontarMarquee();
                }

            }
            catch (Exception ex)
            {
                ShowWindowAlert("Desculpe-nos!<br>O sistema esta temporariamente fora do ar, retorne em alguns minutos.");
            }
        }

        private void MantemLogin()
        {
            
            //if (Session["userSite"] != null)
            //{
            //    hdnIdUsuarioLogado.Value = ((Code.Entity.Usuario)Session["userSite"]).idUsuario.ToString();
            //}

            //if (hdnIdUsuarioLogado.Value.ToString() != "" && Session["userSite"] == null)
            //{ 
            //    using (Code.Data.Usuario oUsuario = new Amoo.Code.Data.Usuario())
            //    {
            //        Session["userSite"] = oUsuario.Get(Convert.ToInt32(hdnIdUsuarioLogado.Value.ToString()));
            //    }
            //}

            if (fncPaginaRestrita())
            { 
                if (Session["userSite"] == null)
                {
                    ShowWindowAlert("Seu login expirou, por favor recarregue a pagina para continuar navegando no site");
                }
            }

        }

        private bool fncPaginaRestrita()
        {
            string[] argRestrita = new string[]{"CadastroBannerHome","Acessos", "/Comunidade/"};


            foreach (string s in argRestrita)
            {
                if (Page.Request.Url.ToString().IndexOf(s) > -1)
                {
                    return true;
                }
            }

            return false;

        }

        private void MontarMarquee()
        {
            Code.Data.Noticia oNoticia = new Amoo.Code.Data.Noticia();
            string strMarquee = "";

            List<Code.Entity.Noticia> retList = oNoticia.GetForMarquee(Convert.ToInt32(Request["idComunidade"]));
            oNoticia.Dispose();

            strMarquee = "<marquee>";
            foreach (Code.Entity.Noticia eNoticia in retList)
            {
                strMarquee += "<a href='/visualizar/comunidade.aspx?idComunidade=" + eNoticia.idComunidade.ToString() + "&idNoticia=" + eNoticia.idNoticia.ToString() + "'><font class='marquee'>" + eNoticia.dsTitulo + "</font></a>";
                strMarquee += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
            }
            strMarquee += "</marquee>";

            dvMarquee.InnerHtml = strMarquee;
        }

        protected void ShowWindowAlert(string pMessage)
        {

            string strHtml = "";

            strHtml += "<div id='dvWindowAlert' style='background-color: #999999; position:absolute; top:0px; left:0px; width:100%; height:100%; filter:alpha(opacity=40);'>";
            strHtml += "    <div style='border: 2px solid #ccffcc; position:absolute; top:35%; left:30%;; background-color: #FFFFFF; width:500px; height:100px;  font-family: Arial, Helvetica, sans-serif; font-size: 17px; color: #000000;'>";
            strHtml += "        <div style='position:absolute; background-color:#ccffcc; width:100%; height:20px;'>";
            strHtml += "            <div style='position:absolute; left:95%'></div>";
            strHtml += "        </div>";
            strHtml += "        <div style='position:absolute; top:35%; left:10px'><img src='/Images/Erro.jpg' width='40px' height='40px'></div>";
            strHtml += "        <div style='position:absolute; top:25%; left:70px'>" + pMessage + "</div>";
            strHtml += "    </div>";
            strHtml += "<script language=javascript>";
            strHtml += "    function closeWindowAlert()";
            strHtml += "    {";
            strHtml += "        document.getElementById('dvWindowAlert').style.visibility='hidden';";
            strHtml += "    }";
            strHtml += "</script>";
            strHtml += "</div>";

            Page.RegisterStartupScript("WindowAlert", strHtml);
        }

    }
}
